<template name="reward">
	<view>
		<view class="cu-modal bottom-modal" :class="modalName=='rankModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog bg bg-white" @tap.stop="">
				<view class="bg-gray space-between padding-sm">
					<view class="">TOP10</view>
					<view class="">打赏数</view>
				</view>
				<view class="rank-view margin-lr">
					<view class="rank-item space-between" v-for="(item,index) in rank_list" v-bind:key="index">
						<view class="n" :class="'n'+(index+1)">{{index+1}}</view>
						<view class="avatar cu-avatar default_avatar round">
							<image v-if="item.member && item.member.head_portrait" :src="item.member.head_portrait" mode="aspectFill"></image>
						</view>
						<view class="name text-left">
							<text v-if="item.member">{{item.member.nickname}}</text>
						</view>
						<view class="count">
							<text class="">{{item.total}}</text>
						</view>
					</view>
					<view class="cu-load margin-center" :class="loading?'loading':''"></view>
				</view>
				<view class="space-between padding-sm align-center border-top" v-if="member">
					<view class="a align-center">
						<image :src="member.head_portrait" mode="aspectFill" class="avatar round margin-right-sm"></image>
						{{member.nickname}}
					</view>
					<view class="cu-btn bg-red text-white round" @tap="modalName='rewardModal'">打赏</view>
				</view>
			</view>
		</view>
		<view class="cu-modal bottom-modal" :class="modalName=='rewardModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog bg bg-white" @tap.stop="">
				<view class="">
					<view class="text-xl text-bold text-black margin-lr margin-top">请选择打赏积分数量</view>
					<view class="reward-money-view margin-top">
						<view class="grid col-3">
							<view @tap="selectMoney(index, item)" class="reward-money-item" :class="curMoney==index?'cur':''" v-for="(item,index) in rewardMoney"
							 v-bind:key="index"><text>{{item}}</text></view>
						</view>
					</view>
					<view class="margin-top text-lg text-bold text-left margin-lr">
						自定义数量
					</view>
					<view class="margin-top margin-lr">
						<input name="inputMoney" v-model="inputMoney" class="inputMoney" type="number" step="1" @input="moneyInputHandle"
						 @blur="commentBlur()" />
					</view>
					<!-- <view class="margin-top margin-lr text-gray tips text-left">
						您好，打赏是自愿赠送给管理员或平台的虚拟商品，且为用户自愿行为。付完成后，除因法定情形规定或者违反其他相关规定的情况外，支付金额支持退款或者兑换。
					</view> -->
					<view class="text-left margin-top-sm margin-lr">当前剩余积分{{member?member.account.user_integral:'-'}}</view>
					<view class="margin-tb">
						<view class="text-red">{{error}}</view>
						<view class="flex col-2 margin-lr">
							<view class="flex-sub">
								<view class="cu-btn line-blue round lg w100" @tap="goPage('/pages/member/interval/recharge')">充值</view>
							</view>
							<view class="flex-twice padding-left">
								<view class="cu-btn bg-blue  round lg w100" @tap="submitReward">打赏</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "reward",
		props: {
			id: {
				type: String,
				default: '0'
			}
		},
		data() {
			return {
				modalName: null,
				rewardMoney: [
					8, 18, 28, 88, 128, 288
				],
				curMoney: null,
				chooseMoney: 0,
				inputMoney: "",
				error: '',
				loading: false,
				rank_list: [],
				rank_list2: [],
				scrollLeft: 0,
				TabCur: 0,
				member: null,
			}
		},
		destroyed() {},
		created: function(e) {
		},
		methods: {
			getRank() {
				this.loading = true
				this.$http
					.get('marketing_auction_pay_reward_rank', {
						id: this.id
					})
					.then((response) => {
						this.rank_list = response.data.data
						this.loading = false
					})
					.catch((response) => {
						console.log(response)
					})
			},
			mini_pay(group, data) {
				this.$miniProgramPay.createPay(group, data)
					.then((response) => {
						console.log(response)
						this.$common.showSuccess('打赏成功', (res) => {
							this.modalName = null
							this.rewardChange(data)
						})
						uni.hideLoading()
						this.loading = false
					})
					.catch((response) => {
						uni.hideLoading()
						console.log(response)
						this.loading = false
					})
			},
			moneyInputHandle(e) {
				e.detail.value = parseInt(e.detail.value)
				this.$nextTick(() => {
					this.inputMoney = parseInt(e.detail.value)
				})
			},
			submitReward() {
				this.error = ''
				let _this = this
				let money = 0
				if (parseFloat(this.inputMoney) > 0) {
					money = parseFloat(this.inputMoney)
				} else money = this.chooseMoney

				if (money > 0) {
					if (this.member.account.user_integral - money >= 0) {
						uni.showLoading({
							title: '加载中...'
						})
						this.$http
							.post('marketing_auction_pay_reward', {
								id: this.id,
								money: money
							})
							.then((response) => {
								this.$common.showSuccess('打赏成功')
								uni.hideLoading()
								this.hideModal()
								this.$common.rmCache('member')
								this.rewardChange({
									money: money
								})
							})
							.catch((response) => {
								console.log(response)
								uni.hideLoading()
							})
					} else {
						uni.showModal({
							title: '系统提示',
							content: '积分不够' + this.detail.interval + ',剩余' + member.account.user_integral + '，请充值后在操作',
							confirmText: '去充值',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/member/interval/recharge'
									})
								}
							}
						})
					}
				} else {
					this.error = '数量不能为0'
				}
			},
			openModal(name) {
				this.$common.getMember().then((member) => {
					this.member = member
				})
				this.modalName = name
				if (this.modalName == 'rankModal') {
					this.getRank()
				}
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
				if (this.modalName == 'rankModal') {
					this.getRank()
				}
			},
			commentBlur() {
				window.scroll(0, 0)
			},
			hideModal(e) {
				this.modalName = null
			},
			selectMoney(index, money) {
				this.curMoney = index
				this.chooseMoney = money
			},
			rewardChange(data) {
				this.$emit("pRewardChange", data)
			},
		}
	}
</script>

<style>
	.space-between {
		display: flex;
		justify-content: space-between;
	}

	.bg {
		border-radius: 30rpx 30rpx 0 0 !important;
		overflow: hidden;
	}

	.reward-money-item {
		text-align: center;
	}

	.reward-money-item text {
		width: 80%;
		height: 70rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #ffe7c2;
		color: #333;
		margin: 10rpx auto;
		border-radius: 30rpx;
		font-weight: bold;
	}

	.reward-money-item.cur text {
		background-color: #fdb242;
		color: #FFF;
	}

	.inputMoney {
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		border-bottom: 1px solid #EEE;
		text-align: left;
		font-size: 32rpx;
	}

	.tips {
		color: #D0D0D0;
	}

	.btn-reward {
		width: 80%;
		padding: 40rpx 0;
	}

	.rank-view {
		height: 50vh;
		overflow: auto;
	}

	.rank-item {
		height: 100rpx;
		overflow: hidden;
		align-items: center;
		margin: 20rpx 0;
		font-size: 30rpx;
	}

	.rank-item .n {
		width: 30rpx;
		min-width: 40rpx;
		color: #B4B4B4;
	}

	.rank-item .n1 {
		color: #e80909;
	}

	.rank-item .n2 {
		color: #FF7200;
	}

	.rank-item .n3 {
		color: #F5D501;
	}

	.rank-item .avatar {
		width: 100rpx;
		height: 100rpx;
		min-width: 100rpx;
		margin: 10rpx;
	}

	.rank-item .avatar image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.rank-item .name {
		width: 100%;
		font-weight: bold;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.rank-item .count {
		width: 160rpx;
		min-width: 160rpx;
		text-align: right;
		font-weight: bold;
	}

	.a {
		display: flex;
	}

	.avatar {
		width: 60rpx;
		height: 60rpx;
	}

	.border-top {
		border-top: 1px solid #EEE;
	}
</style>
